<template>
  <div class="wrap-item">
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model.trim="formSearch.param.phone"
          placeholder="请输入账号"
          maxlength="11"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.userItemType"
          placeholder="请选择类型"
        >
          <el-option label="用户" value="1"></el-option>
          <el-option label="企业" value="2"></el-option>
          <el-option label="专家" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.userStatus"
          placeholder="请选择状态"
        >
          <el-option label="启用" value="0"></el-option>
          <el-option label="停用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span
        ><span
          class="wrap-btn wrap-warning"
          @click="download"
          v-if="$checkPermission(108)"
          >导出</span
        >
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-item-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column prop="accountName" label="账号"></el-table-column>
        <el-table-column label="类型">
          <template slot-scope="scope">
            <span v-if="scope.row.userItemType == 1">用户</span>
            <span v-if="scope.row.userItemType == 2">企业</span>
            <span v-if="scope.row.userItemType == 3">专家</span>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="联系电话"></el-table-column>
        <el-table-column label="所属角色">
          <template slot-scope="scope">
            <span v-for="(item, index) in scope.row.roles" :key="index">
              {{
                item.roleName + (scope.row.roles.length == index + 1 ? '' : ',')
              }}
            </span>
          </template></el-table-column
        >
        <el-table-column
          prop="addTime"
          label="注册时间"
          width="160px"
        ></el-table-column>
        <el-table-column label="状态" width="80px" align="center">
          <template slot-scope="scope">
            <el-link
              type="success"
              :underline="false"
              v-if="scope.row.userStatus == 0"
              >启用</el-link
            >
            <el-link
              type="danger"
              :underline="false"
              v-if="scope.row.userStatus == 1"
              >停用</el-link
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item
                  command="changeStatus"
                  v-if="scope.row.userStatus == 1 && $checkPermission(105)"
                  >启用</el-dropdown-item
                >
                <el-dropdown-item
                  command="changeStatus"
                  v-if="scope.row.userStatus == 0 && $checkPermission(106)"
                  >停用</el-dropdown-item
                >
                <el-dropdown-item
                  command="subscribe"
                  v-if="$checkPermission(101)"
                  >查看事件</el-dropdown-item
                >
                <el-dropdown-item command="sync" v-if="$checkPermission(104)"
                  >同步至子系统</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
    <!-- 查看 -->
    <el-dialog title="查看" :visible.sync="isLook" width="888px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="姓名">
          {{ lockData.userName }}
        </el-descriptions-item>
        <el-descriptions-item label="用户分组">
          <i v-for="(item, index) in lockData.groupId" :key="index">
            {{
              item.groupName + (lockData.groupId.length == index + 1 ? '' : ',')
            }}
          </i>
        </el-descriptions-item>
        <el-descriptions-item label="联系电话">
          {{ lockData.phone }}
        </el-descriptions-item>
        <el-descriptions-item label="所在组织机构">
          <i v-for="(item, index) in lockData.orgIds" :key="index">
            {{
              item.orgName + (lockData.orgIds.length == index + 1 ? '' : ',')
            }}
          </i>
        </el-descriptions-item>
        <el-descriptions-item label="角色" :span="2">
          <i v-for="(item, index) in lockData.roleIds" :key="index">
            {{
              item.roleName + (lockData.roleIds.length == index + 1 ? '' : ',')
            }}
          </i>
        </el-descriptions-item>
        <el-descriptions-item label="工号">
          {{ lockData.workNum }}
        </el-descriptions-item>
        <el-descriptions-item label="职位">
          {{ lockData.workPost }}
        </el-descriptions-item>
        <el-descriptions-item label="邮箱" :span="2">
          {{ lockData.email }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: {},
      },
      total: null,
      tableData: [],
      visible: false,
      isLook: false,
      lockData: {},
    }
  },
  created() {
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios.post('/admin/member/page', this.formSearch).then((data) => {
        this.tableData = []
        if (data.code == 200) {
          this.tableData = data.data.list
          this.total = data.data.total
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          //查看
          this.$router.push({
            path: '/userCenter/member/detail',
            query: { id: row.id },
          })
          break
        case 'changeStatus':
          //启用停用
          const status = row.userStatus == 0 ? 1 : 0
          this.$axios
            .get(
              '/admin/member/updateStatus?id=' + row.id + '&status=' + status
            )
            .then((data) => {
              if (data.code == 200) {
                this.itemList()
                this.$message.success('操作成功！')
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'subscribe':
          //查看事件
          this.$router.push({
            path: '/userCenter/member/subscribe',
            query: { id: row.id },
          })
          break
        case 'sync':
          //同步至子系统
          this.$axios
            .get('/admin/member/sendUser?id=' + row.id)
            .then((data) => {
              if (data.code == 200) {
                this.$message.success('操作成功！')
              } else {
                this.$message.error(data.msg)
              }
            })
          break
      }
    },
    download() {
      //导出
      this.$axios
        .post('/admin/member/exports', this.formSearch, {
          responseType: 'blob',
        })
        .then((data) => {
          const reader = new FileReader()
          reader.readAsDataURL(data)
          reader.onload = function (e) {
            const a = document.createElement('a')
            a.download = '会员用户'
            a.href = e.target.result
            a.click()
          }
        })
    },
  },
}
</script>
